package com.sinochem.iot.rule.service;

import com.alibaba.fastjson.JSON;
import com.sinochem.iot.rule.common.ConstMap;
import com.sinochem.iot.rule.common.TriggerDescEnum;
import com.sinochem.iot.rule.mapper.TriggerMapper;
import com.sinochem.iot.rule.mapper.TriggerRuleDetailMapper;
import com.sinochem.iot.rule.mapper.TriggerRuleMapper;
import com.sinochem.iot.rule.mapper.TriggerRuleStoreMapper;
import com.sinochem.iot.rule.model.*;
import com.sinochem.iot.common.util.BeanHelper;
import com.sinochem.iot.rule.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class TriggerRuleDetailService {

    @Autowired
    private TriggerRuleDetailMapper triggerRuleDetailMapper;
    @Autowired
    private TriggerMapper triggerMapper;
    @Autowired
    private TriggerRuleMapper triggerRuleMapper;
    @Autowired
    private TriggerRuleStoreMapper triggerRuleStoreMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 根据规则id修改状态
     *
     * @param triggerRuleDetail
     */
    public void updateStatusByRuleId(TriggerRuleDetail triggerRuleDetail) {
        //删除redis
        List<TriggerRuleDetail> detailList = getTriggerRuleDetailListByRuleId(triggerRuleDetail.getRuleId());
        for (TriggerRuleDetail detail : detailList) {
            String propertyIdentify = detail.getUseridentifyProductidentifyDeviceidentify();
            if (redisTemplate.hasKey(propertyIdentify))
                redisTemplate.delete(propertyIdentify);
        }
        //修改detail表中的状态
        triggerRuleDetail.setUpdateTime(new Date());
        triggerRuleDetailMapper.updateStatusByRuleId(triggerRuleDetail);
    }

    /**
     * 添加表t_trigger_rule_detail
     *
     * @param triggerRule
     */
    public void insertByTriggerRuleStore(TriggerRule triggerRule) {
        Integer ruleId = triggerRule.getId();

        //获取触发器
        Trigger trigger = triggerMapper.selectByPrimaryKey(triggerRule.getTriId());

        String[] arrDeviceIdentify = triggerRule.getQueryScopDev().split(",");
        List<TriggerRuleDetail> list = new ArrayList<>();

        //先根据规则id删除
        triggerRuleDetailMapper.deleteByRuleId(ruleId);

        //循环设备标识添加到t_trigger_rule_detail
        insertRuleDetailList(triggerRule, ruleId, trigger, arrDeviceIdentify, list);
        //批量添加
        triggerRuleDetailMapper.insertBatch(list);

    }

    /**
     * 修改触发规则时候保存t_trigger_rule_detail
     *
     * @param triggerRule
     */
    public void insertByTriggerRule(TriggerRule triggerRule) {
        Integer ruleId = triggerRule.getId();
        //获取触发器
        Trigger trigger = triggerMapper.selectByPrimaryKey(triggerRule.getTriId());
        //获取存储位置
        TriggerRuleStoreExample triggerRuleStoreExample = new TriggerRuleStoreExample();
        triggerRuleStoreExample.createCriteria().andRuleIdEqualTo(ruleId);
        List<TriggerRuleStore> storeList = triggerRuleStoreMapper.selectByExample(triggerRuleStoreExample);
        if (storeList.size() > 0) {
            TriggerRuleStore triggerRuleStore = storeList.get(0);

            String[] arrDeviceIdentify = triggerRule.getQueryScopDev().split(",");
            List<TriggerRuleDetail> list = new ArrayList<>();

            //先根据规则id删除
            triggerRuleDetailMapper.deleteByRuleId(ruleId);

            //循环设备标识添加到t_trigger_rule_detail
            insertRuleDetailList(triggerRule, ruleId, trigger, arrDeviceIdentify, list);
            //批量添加
            triggerRuleDetailMapper.insertBatch(list);
        }
    }

    /**
     * 根据规则id删除
     *
     * @param ruleId
     */
    public void deleteByRuleId(Integer ruleId) {
        //删除redis
        List<TriggerRuleDetail> detailList = getTriggerRuleDetailListByRuleId(ruleId);
        for (TriggerRuleDetail detail : detailList) {
            String propertyIdentify = detail.getUseridentifyProductidentifyDeviceidentify();
            if (redisTemplate.hasKey(propertyIdentify))
                redisTemplate.delete(propertyIdentify);
        }
        triggerRuleDetailMapper.deleteByRuleId(ruleId);
    }

    /**
     * 根据ruleId获取TriggerRuleDetail列表
     *
     * @param ruleId
     * @return
     */
    public List<TriggerRuleDetail> getTriggerRuleDetailListByRuleId(Integer ruleId) {
        TriggerRuleDetailExample triggerRuleDetailExample = new TriggerRuleDetailExample();
        triggerRuleDetailExample.createCriteria().andRuleIdEqualTo(ruleId);
        return triggerRuleDetailMapper.selectByExample(triggerRuleDetailExample);
    }

    private void insertRuleDetailList(TriggerRule triggerRule, Integer ruleId, Trigger trigger, String[] arrDeviceIdentify, List<TriggerRuleDetail> list) {
        for (String deviceIdentify : arrDeviceIdentify) {
            TriggerRuleDetail triggerRuleDetail = new TriggerRuleDetail();

            triggerRuleDetail.setRuleId(ruleId);
            triggerRuleDetail.setStatus(trigger.getStatus());
            String userProDevIdentify = trigger.getUsername() + "##" + triggerRule.getQueryScopPro() + "##" + deviceIdentify;
            triggerRuleDetail.setUseridentifyProductidentifyDeviceidentify(userProDevIdentify);


            //添加到redis中
            TriggerRuleDetailExample triggerRuleDetailExample = new TriggerRuleDetailExample();
            triggerRuleDetailExample.createCriteria().andUseridentifyProductidentifyDeviceidentifyEqualTo(userProDevIdentify);
            List<TriggerRuleDetail> triggerRuleDetailList = triggerRuleDetailMapper.selectByExample(triggerRuleDetailExample);

            RedisUtil.setObject(userProDevIdentify, triggerRuleDetailList);

            //添加到list
            BeanHelper.onInsert(triggerRuleDetail);
            list.add(triggerRuleDetail);
        }
    }
}
